package org.phalanx.servlet.webmessages;

import java.util.ArrayList;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WebContextMessageManager {
	private ArrayList<WebMessageable> contextClassList;
	
	public WebContextMessageManager(){
		contextClassList = new ArrayList<WebMessageable>();
	}
	
	public void addContextListener(WebMessageable listenerClass){
		if (!contextClassList.contains(listenerClass)){
			contextClassList.add(listenerClass);
		}
	}
	
	public void removeContextListener(WebMessageable listenerClass){
		if (contextClassList.contains(listenerClass)){
			contextClassList.remove(listenerClass);
		}
	}
	
	public ArrayList<WebMessageable> getListenerList(){
		return contextClassList;
	}
	
	public void sendContextToListeners(String context, Cookie auth, HttpServletRequest req, HttpServletResponse resp){
		for (int i = 0; i < contextClassList.size(); i++){
			WebMessageable listener = contextClassList.get(i);
			
			listener.onContextLoadable(context, auth, req, resp);
		}
	}
}
